package edu.henu.personnel.controller;

import com.github.pagehelper.PageInfo;
import edu.henu.personnel.entity.Catalog;
import edu.henu.personnel.entity.Notice;
import edu.henu.personnel.service.catalog.CatalogService;
import edu.henu.personnel.service.notice.NoticeService;
import edu.henu.personnel.utils.DateUtil;
import edu.henu.personnel.utils.FilePathAndName;
import edu.henu.personnel.utils.FileUtil;
import edu.henu.personnel.vo.NoticeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: PersonnelProject
 * @description: 公告控制器
 * @author: zhuyang
 * @create: 2019-05-13 11:46
 */

@Controller
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    @Autowired
    private CatalogService catalogService;

    @GetMapping(value = "/notices/{pageNum}")
    public String list(@PathVariable("pageNum") Integer pageNum,
                       Model model) {
        System.out.println("进入notices控制器");
        PageInfo<NoticeVO> notices = noticeService.getAllNotice(pageNum);
        model.addAttribute("notices", notices);
        return "notice/list";
    }

    @GetMapping(value = "/noticeInCatalog/{cid}/{pageNum}")
    public String noticeInCatalog(@PathVariable("cid") Integer cid, @PathVariable("pageNum") Integer pageNum,
                                  Model model) {
        PageInfo<NoticeVO> noticeList = noticeService.getNoticeListByCatelogid(cid, pageNum);
        model.addAttribute("noticeList", noticeList);
        return "catalog/noticeInCatalog";
    }

    @GetMapping("/notice")
    public String toAddNotice(Model model) {
        List<Catalog> catalogList = catalogService.getAllCatalogInfo();
        List<Catalog> catalogs = new ArrayList<>();

        for (Catalog catalog : catalogList) {
            if (!(catalog.getCmemo().equals("下载中心") || (catalog.getCname().equals("人才队伍")))) {
                catalogs.add(catalog);
            }
        }

        model.addAttribute("catalogList", catalogs);
        return "notice/add";
    }

    @PostMapping("/notice")
    public String addNotice(Notice notice, @RequestParam("fileList") MultipartFile[] fileList,
                            Model model) {

        if (fileList.length > 6) {
            return "redirect:/notice";
        }

        // 获取当前时间[yyyyMMddHHmmss]，为数据库中t_notice表中的nid赋值
        String fileDate = DateUtil.getFileDate();
        notice.setNid(fileDate);

        // 获取当前时间 [yyyy-MM-dd HH:mm:ss]，为数据库中t_notice表中的ndate赋值
        String storeDate = DateUtil.getStoreDate();
        notice.setNdate(storeDate);

        // 初始化notice的浏览量
        notice.setNbrowses(0);

        int result = 0;
        // 附件上传保存操作
        String storeFilePath = FileUtil.fileListPath + notice.getNid() + "/";
        try {
            String[] filenameList = FileUtil.fileListUpload(fileList, storeFilePath, fileDate);
            notice.setNappendix1(filenameList[0]);
            notice.setNappendix2(filenameList[1]);
            notice.setNappendix3(filenameList[2]);
            notice.setNappendix4(filenameList[3]);
            notice.setNappendix5(filenameList[4]);
            notice.setNappendix6(filenameList[5]);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            result = noticeService.addNotice(notice);
        }

        if (result == 1) {
            // 添加成功跳转
            return "redirect:/notices/1";
        } else {
            model.addAttribute("returnMsg", "添加失败");
            return "redirect:/notices/1";
        }
    }

    @GetMapping("/notice/{nid}")
    public String toEditPage(@PathVariable("nid") String nid, Model model) {
        Notice notice = noticeService.getNoticeById(nid);

        List<String> filePathList = FileUtil.getFilePath(notice);
        List<FilePathAndName> fileList = FileUtil.getFilePathAndName(filePathList);

        model.addAttribute("notice", notice);
        model.addAttribute("fileList", fileList);

        List<Catalog> catalogList = catalogService.getAllCatalogInfo();
        model.addAttribute("catalogList", catalogList);

        return "notice/modify";
    }

    @PutMapping("/notice")
    public String updateNotice(Notice notice, Model model) {
        Notice tempNotice = noticeService.getNoticeById(notice.getNid());
        tempNotice.setNtitle(notice.getNtitle());
        tempNotice.setCid(notice.getCid());
        tempNotice.setNuser(notice.getNuser());
        tempNotice.setNcontent(notice.getNcontent());
        int result = noticeService.modifyNoticeInfo(tempNotice);
        return "redirect:/notices/1";
    }

    @DeleteMapping("/notice/{nid}")
    public String deleteNotice(@PathVariable("nid") String nid, Model model) {
        int result = noticeService.deleteNoticeInfo(nid);
        model.addAttribute("deleteNoticeResult", result);
        return "redirect:/notices/1";
    }

    @DeleteMapping("/noticeAppendix/{fileIndex}")
    public String deleteAppendix(@RequestParam("nid") String nid,
                                 @PathVariable("fileIndex") Integer fileIndex) {
        System.out.println(nid);
        System.out.println(fileIndex);
        return "redirect:/notice/" + nid;
    }
}
